package com.wechat.controller;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wechat.kit.SecurityKit;

@Controller
public class WechatController {
	public static final String TOKEN="weixin";
	
	@RequestMapping("/init")
	public void init(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		String signature=req.getParameter("signature");
		String timestamp=req.getParameter("timestamp");
		String nonce=req.getParameter("nonce");
		String echostr=req.getParameter("echostr");
		String [] arr={WechatController.TOKEN,timestamp,nonce};
		Arrays.sort(arr);
		StringBuffer sb=new StringBuffer();
		for(String a:arr){
			sb.append(a);
		}
		String sha1=SecurityKit.sha1(sb.toString());
		if(sha1.equals(signature)){
			System.out.println(sha1.equals(signature));
			resp.getWriter().print(echostr);
		}
	}

}
